package com.jsz.peini.controller.app;

import java.util.Date;
import java.util.ResourceBundle;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jsz.peini.bean.app.AppActivityDev;
import com.jsz.peini.bean.app.AppActivityUser;
import com.jsz.peini.bean.app.AppStartPageHis;
import com.jsz.peini.bean.app.SysConfig;
import com.jsz.peini.bean.user.UserInfo;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.common.util.UploadQN;
import com.jsz.peini.emchat.api.EasemobIMUsers;
import com.jsz.peini.mapper.app.AppActivityDevMapper;
import com.jsz.peini.mapper.app.AppActivityUserMapper;
import com.jsz.peini.mapper.app.AppStartPageHisMapper;
import com.jsz.peini.mapper.app.SysConfigMapper;
import com.jsz.peini.mapper.user.UserInfoMapper;

/**
 * @author Han Guohong
 * @version 2017-3-16 下午9:28:54
 * 类说明
 */
@Controller
public class AppController {
	private static final Logger logger = LoggerFactory.getLogger(AppController.class);
	@Resource
	private AppActivityDevMapper appActivityDevMapper;
	@Resource
	private AppStartPageHisMapper appStartPageHisMapper;
	@Resource
	private AppActivityUserMapper appActivityUserMapper;
	@Resource
	private UserInfoMapper userInfoMapper;
	@Resource
	private SysConfigMapper sysConfigMapper;

	@ResponseBody
	@RequestMapping("/app/appStart")
	public String dev_activity(HttpServletRequest request,HttpServletResponse response,
			@RequestParam(value="devNo") String devNo,
			@RequestParam(value="versionNo",required=false,defaultValue="10") String versionNo,
			@RequestParam(value="channel",required=false,defaultValue="") String channel,
			@RequestParam(value="devtype",required=false) Integer devtype,
			@RequestParam(value="userId",required=false) String userId
			){
		JSONObject object = new JSONObject();
		try {
			
			request.getSession().setAttribute("devNo", devNo);
			request.getSession().setAttribute("versionNo", versionNo);
			request.getSession().setAttribute("channel", channel);
			request.getSession().setAttribute("devtype", devtype);
			
			logger.info(request.getSession().getAttribute("devNo").toString()+new Date()+"==<<userId>>=="+userId+"==<<versionNo>>=="+versionNo);
			
			AppActivityDev dev = appActivityDevMapper.selectByPrimaryKey(devNo);
			if(dev == null){
				dev = new AppActivityDev();
				dev.setDevNo(devNo);
				dev.setCreatetime(new Date());
				if(StringUtils.isNotBlank(channel)){
					dev.setChannel(channel);
				}
				if(devtype != null){
					dev.setDevtype(devtype);
				}
				appActivityDevMapper.insert(dev);
			}
			
			AppStartPageHis his = new AppStartPageHis();
			his.setCreatetime(new Date());
			his.setDevNo(devNo);
			if(StringUtils.isNotBlank(channel)){
				his.setChannel(channel);
			}
			if(devtype != null){
				his.setDevtype(devtype);
			}
			his.setId(UUID.randomUUID().toString().replace("-", ""));
			appStartPageHisMapper.insert(his);
			
			JSONObject obj = PeiniUtils.validToken(userId);
			if(obj.getInteger("resultCode") != 1){
				
			}else{
				userId = obj.getString("userId");
				AppActivityUser user = new AppActivityUser();
				user.setCreatetime(new Date());
				user.setId(UUID.randomUUID().toString().replace("-", ""));
				user.setUserid(userId);
				if(StringUtils.isNotBlank(channel)){
					user.setChannel(channel);
				}
				if(devtype != null){
					user.setDevtype(devtype);
				}
				user.setDevNo(devNo);
				user.setVersionNo(versionNo);
				appActivityUserMapper.insert(user);
				
				//TODO 将来去掉
				/*UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userId);
				ObjectNode datanode = JsonNodeFactory.instance.objectNode();
		        datanode.put("username",userInfo.getUserPhone());
		        datanode.put("password", "123456");
		        datanode.put("nickname", userInfo.getNickname());
		        EasemobIMUsers.createNewIMUserSingle(datanode);*/
			}
			
			JSONObject dataObj = new JSONObject();
			
			SysConfig config = sysConfigMapper.selectByPrimaryKey(1);
			dataObj.put("opVal", config.getConval());
			
			config = sysConfigMapper.selectByPrimaryKey(2);
			dataObj.put("opT", config.getConval());
			
			object.put("data", dataObj);
			
			object.put("resultCode", Constants.SUCCESS);
			object.put("resultDesc", Constants.SUCCESS_DESC);
			//return object.toString();
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		//logger.info(object.toJSONString());
		return object.toString();
	}
}
